package com.hzzh.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzzh.service.EmployeeService;
import com.hzzh.vo.Employee;

/**
 * Servlet implementation class SearchEmployeesServlet
 */
@WebServlet("/SearchEmployeesServlet")
public class SearchEmployeesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchEmployeesServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		//获得输入的查询条件
		String employeename = request.getParameter("employeename");
		String username = request.getParameter("username");
		String status = request.getParameter("status");
		EmployeeService service = new EmployeeService();
		//跳转当前页码
		String pageNumStr = request.getParameter("pageNum");
		int pageNum = 0;
		if(pageNumStr == null || pageNumStr.equals("")){
			pageNum = 1;
		}else{
			pageNum = Integer.parseInt(pageNumStr);
		}
	    int pageSize = service.getPageSizeService();
		try {
			//得到结果集
			//获得所有记录数量，先调用DAO中的search方法
			ArrayList<Employee> list = service.selectEmployeesOfOnePageService(employeename, username, status, pageSize, pageNum);
			service.selectEmployeesByNameStatuService(employeename, username, status);
			request.setAttribute("employeesList", list);
			request.setAttribute("search", "1");
			service.selectEmployeesOfOnePageService(employeename, username, status, pageSize, pageNum);
			//获取信息总页数
			int countOfPages = service.getCountOfPagesService();
			request.setAttribute("countOfPages", countOfPages);
			//获取信息总条数
			int countOfEmployees = service.getCountOfEmployeesService();
			request.setAttribute("countOfEmployees", countOfEmployees);

			//当前页数
            request.setAttribute("pageNum", pageNum);
			request.getRequestDispatcher("searchemployees.jsp").forward(request, response);
		}
		catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
